﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Global;

namespace BIS.Portal.Modules.CMS.Adm
{
    public partial class AdminCmsDisplayTypeControl : ControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (hdfAdminCmsDisplayType_IsPostBack.Value.Equals("false", StringComparison.OrdinalIgnoreCase))
            {
                BindDataGridView();
            }      
            btnDelete.Attributes.Add("onclick", "return confirm('Are u sure u want to delete?');");
            
        }

        #region Bind Data GriedView
        public void BindDataGridView()
        {
            IList<AdminCmsDisplayTypeEntity> list = AdminCmsDisplayTypeBO.GetAllDisplayType();
            grvDisplayType.DataSource = list;
            grvDisplayType.DataKeyNames = new string[] { "display_type_id" };
            grvDisplayType.DataBind();

            Literal ltrTemp;
            for (int i = 0; i < grvDisplayType.Rows.Count; i++)
            {
                ltrTemp = grvDisplayType.Rows[i].FindControl("ltrStatus") as Literal;
                if (list[i].Status == 0 && ltrTemp != null)
                {
                    ltrTemp.Text = "Tạm ẩn";
                }
                if (list[i].Status == 1 && ltrTemp != null)
                {
                    ltrTemp.Text = "Hiển thị";
                }
                if (list[i].Status == 99 && ltrTemp != null)
                {
                    ltrTemp.Text = "Xoá logic";
                }
            }
        }

        #endregion

        #region FORM
        public AdminCmsDisplayTypeEntity GetDataForm()
        {
            AdminCmsDisplayTypeEntity entity;
            if (btnAddNew.Enabled == true)
            {
                entity = new AdminCmsDisplayTypeEntity();
                entity.Last_update = DateTime.Now;
                entity.Last_update_by = "System";
            }
            else
            {
                int id = SessionDisplayTypeId;
                entity = AdminCmsDisplayTypeBO.GetByDisplayTypeId(id);
            }
            entity.Display_type_name = txtDisplayTypeName.Text;
            entity.Status = int.Parse(ddlStatus.SelectedValue);

            return entity;
        }

        public void FillForm(int displayTypeId)
        {
            if (displayTypeId < 1)
                return;

            AdminCmsDisplayTypeEntity entity = AdminCmsDisplayTypeBO.GetByDisplayTypeId(displayTypeId);
            if (entity == null || entity.Display_type_id == 0)
            {
                ltrMessage.Text = "Lấy thông tin loại hiển thị thất bại !";
            }
            txtDisplayTypeName.Text = entity.Display_type_name;
            ddlStatus.SelectedValue = entity.Status.ToString();
        }

        public void ClearForm()
        {
            txtDisplayTypeName.Text = "";
            ddlStatus.SelectedIndex = 0;
        }
        #endregion

        #region Add New
        private void AddNew()
        {
            if (string.IsNullOrEmpty(txtDisplayTypeName.Text))
            {
                ltrMessage.Text = "Không được bỏ trống tên top hiển thị";
                txtDisplayTypeName.Focus();
                return;
            }
            AdminCmsDisplayTypeEntity entity = GetDataForm();
            if (AdminCmsDisplayTypeBO.Insert(entity))
            {
                ClearForm();
                BindDataGridView();
                ltrMessage.Text = "Thêm mới thành công";
            }
            else
            {
                ltrMessage.Text = "Thêm mới thất bại";
                return;
            }
        }

        #endregion

        #region Update Diplay
        private void UpdateDisplay()
        {
            if (string.IsNullOrEmpty(txtDisplayTypeName.Text))
            {
                ltrMessage.Text = "Không được bỏ trống tên top hiển thị";
                txtDisplayTypeName.Focus();
                return;
            }
            int id = SessionDisplayTypeId;
            if (id < 1)
            {
                ltrMessage.Text = "Lấy thông tin loại hiển thị thất bại !";
            }
            else
            {
                AdminCmsDisplayTypeEntity entity = GetDataForm();
                if (entity == null)
                {
                    ltrMessage.Text = "Cần chọn một dòng trước khi muốn sửa dữ liệu";
                    return;
                }
                if (AdminCmsDisplayTypeBO.Update(entity))
                {
                    ClearForm();
                    BindDataGridView();
                    SessionDisplayTypeId = 0;
                    ChangeMode(false);
                    ltrMessage.Text = "Cập nhật thành công";

                }

            }
        }

        #endregion

        #region Gridview's action
        protected void grvDisplayType_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = GetDisplayTypeIdByRowIndex(e.NewEditIndex);
            FillForm(id);
            ChangeMode(true);
            SessionDisplayTypeId = id;
            ltrMessage.Text = "";
        }

        protected void grvDisplayType_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                AdminCmsDisplayTypeEntity entity = e.Row.DataItem as AdminCmsDisplayTypeEntity;
                HyperLink lbtnManager = e.Row.FindControl("lbtnManager") as HyperLink;

                if (lbtnManager != null)
                {
                    lbtnManager.NavigateUrl = WebHelpers.GetLink(UrlRewriteRules.DisplayType, CmsDisplayTypePageAlias,
                                                             new string[] { QueryStringKeys.DisplayTypeId },
                                                             new string[] { entity.Display_type_id.ToString() });
                }
            }

        }

        protected void grvDisplayType_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvDisplayType.PageIndex = e.NewPageIndex;
            BindDataGridView();
        }

        #endregion

        #region Private function

        private int GetDisplayTypeIdByRowIndex(int rowIndex)
        {
            if (rowIndex > grvDisplayType.DataKeys.Count
               || rowIndex < 0
               || grvDisplayType.DataKeys[rowIndex] == null
               || grvDisplayType.DataKeys[rowIndex].Value == null)
            {
                return 0;
            }

            int displayTypeId = 0;
            int.TryParse(grvDisplayType.DataKeys[rowIndex].Value.ToString(), out displayTypeId);

            return displayTypeId;
        }

        private void ChangeMode(bool isEnable)
        {
            btnAddNew.Enabled = !isEnable;
            btnUpdate.Enabled = isEnable;
            btnCancel.Enabled = isEnable;
        }
        #endregion

        #region Button's Action
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateDisplay();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
            ChangeMode(false);
            SessionDisplayTypeId = 0;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grvDisplayType.Rows.Count; i++)
            {
                CheckBox cb = grvDisplayType.Rows[i].FindControl("chkSell") as CheckBox;
                if (cb != null && cb.Checked)
                {
                    btnDelete.Enabled = true;
                    if (grvDisplayType.DataKeys[i] != null)
                    {
                        int test = Int32.Parse(grvDisplayType.DataKeys[i].Value.ToString());
                        AdminCmsDisplayTypeBO.Delete(test);
                    }
                }
            }
            BindDataGridView();
        }
        #endregion

        public int SessionDisplayTypeId
        {
            get { return (null != Session["SessionDisplayTypeId"] ? int.Parse(Session["SessionDisplayTypeId"].ToString()) : -1); }
            set { Session["SessionDisplayTypeId"] = value; }
        }


        public string CmsDisplayTypePageAlias
        {
            get
            {
                try
                {
                    return GetControlSetting<string>(
                                                     ModuleSettingNames.AdminCmsDisplayTypeControl.AdminCmsDisplayType_ContentPage,
                                                     PortalContext.LanguageAlias);
                }
                catch (Exception)
                {
                    return "";
                }
            }
        }

    }
}